use lazy_static::lazy_static;
use rbase::db::DbColumnDefine;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "t_user_role")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    #[sea_orm(default_value = 0)]
    pub user_id: i64,

    #[sea_orm(default_value = 0)]
    pub role_id: i64,

    #[sea_orm(default_value = 0)]
    pub created_at: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub modified_at: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation, Deserialize, Serialize)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::role::Entity",  // 关联到 Role 实体
        from = "Column::RoleId",
        to = "super::role::Column::Id"
    )]
    Role, // 关系名称
}

impl ActiveModelBehavior for ActiveModel {} // 必须实现这个trait

pub type UserRoleEntity = Entity;
pub type UserRoleModel = Model;
pub type UserRoleActiveModel = ActiveModel;

pub static TABLE_NAME: &str = "t_user_role";
pub static CREATE_QUERY: &str = r#"
    CREATE TABLE IF NOT EXISTS t_user_role (
        id INTEGER NOT NULL PRIMARY KEY,
        user_id INTEGER NOT NULL,
        role_id INTEGER NOT NULL,
        UNIQUE (user_id, role_id)
    );
"#;

lazy_static! {
    pub static ref TABLE_COLUMNS: Vec<DbColumnDefine<'static>> = vec![
        DbColumnDefine::new("created_at", "INTEGER", "ALTER TABLE t_user_role ADD COLUMN created_at INTEGER nullable"),
        DbColumnDefine::new("modified_at", "INTEGER", "ALTER TABLE t_user_role ADD COLUMN modified_at INTEGER nullable"),
    ];
}
